package HelperFinancial.User;

import java.util.List;
import HelperFinancial.DataBase.CategoriaAdapter;
import HelperFinancial.DataBase.ConfigurationAdapter;
import HelperFinancial.DataBase.CreditoHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListarCreditosActivity extends Activity {

	ListView list;

	Cursor model = null;
	CreditoAdapter adapter = null;
	ListarCreditosActivity frame = this;
	CreditoHelper helper;
	CategoriaAdapter categoriaAdapter;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lista_creditos);

		helper = new CreditoHelper(this);
		
		categoriaAdapter = CategoriaAdapter.getInstance(frame);
		
		ConfigurationAdapter configAdapter = ConfigurationAdapter.getInstance(frame);

		list = (ListView) findViewById(R.id.credits);
		model = helper.getCreditsByFilter(configAdapter.getDateInit());
//		if (configAdapter.getCategory().equals("Default")) {
//			model = helper.getCreditsByFilter(configAdapter.getDateInit());
//		} else {
//			model = helper.getCreditsByFilter(configAdapter.getDateInit(), configAdapter.getCategory());
//		}
//		model = helper.getAllCredits();
		startManagingCursor(model);
		adapter = new CreditoAdapter(model);
		list.setAdapter(adapter);
		list.setOnItemClickListener(onListClick);

	}

	private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View row, int position,
				long id) {
			model.moveToPosition(position);
			Intent intent = new Intent(frame, GanhoViewActivity.class);
			Integer id_Ganho = model.getInt(0);
			if (id_Ganho != null && id > 0) {
				intent.putExtra("id", model.getInt(0));
				finish();
				startActivity(intent);
			}

		}

	};

	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_configuration, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.filter) {
			finish();
			startActivity(new Intent(this, DateUpdateActivity.class));
			return true;
		}

		if (item.getItemId() == R.id.redefine_default) {
			redefinirPadrao();
			finish();
			startActivity(new Intent(this, ExtractActivity.class));
			return true;
		}
		return (super.onOptionsItemSelected(item));
	}

	private void redefinirPadrao() {
		ConfigurationAdapter config = ConfigurationAdapter.getInstance(frame);
		config.populate();

	}

	private void selecionarCategoria() {
		List<String> categorias = categoriaAdapter.getAllCategorys();
		final String[] items = new String[categorias.size()];
		for (int i = 0; i < items.length; i++) {
			items[i] = categorias.get(i);
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(frame);
		builder.setTitle("Selecione a Categoria");
		builder.setItems(items, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				String categoria = items[item];
				ConfigurationAdapter config = ConfigurationAdapter.getInstance(frame);
	            String id = config.getId();
	            config.updateCategory(categoria, id);
			}
		});
		AlertDialog alert = builder.create();
		alert.show();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		helper.close();
	}

	class CreditoAdapter extends CursorAdapter {

		CreditoAdapter(Cursor c) {
			super(ListarCreditosActivity.this, c);

		}

		@Override
		public void bindView(View row, Context ctxt, Cursor c) {
			CreditoHolder holder = (CreditoHolder) row.getTag();

			holder.populateFrom(c, helper);
		}

		@Override
		public View newView(Context ctxt, Cursor c, ViewGroup parent) {
			LayoutInflater inflater = getLayoutInflater();
			View row = inflater.inflate(R.layout.credito_row, parent, false);
			CreditoHolder holder = new CreditoHolder(row);
			row.setTag(holder);
			return (row);
		}

	}

	static class CreditoHolder {
		private TextView name = null;
		private TextView value = null;
		private ImageView icon = null;
		private View row = null;

		CreditoHolder(View row) {
			this.row = row;
			name = (TextView) row.findViewById(R.id.credit_name);
			value = (TextView) row.findViewById(R.id.credit_value);
			icon = (ImageView) row.findViewById(R.id.icon);
		}

		void populateFrom(Cursor c, CreditoHelper helper) {
			name.setText(helper.getCreditName(c));
			value.setText(String.valueOf(helper.getCreditValue(c)));
			icon.setImageResource(R.drawable.credito);

		}

	}

}
